package xyz.pro5books.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.pro5books.mapper.BookMapper;
import xyz.pro5books.mapper.BookTempMapper;
import xyz.pro5books.pojo.Book;
import xyz.pro5books.pojo.BookTemp;
import xyz.pro5books.vo.PageResult;

import java.util.Date;
import java.util.List;

@Service
public class BookTempServiceImpl implements BookTempService{

    @Autowired
    private BookTempMapper bookTempMapper;
    @Autowired
    private BookMapper bookMapper;


    @Override
    public PageResult getBookTempList(PageResult pageResult) {
        IPage iPage = new Page(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        boolean flag = StringUtils.hasLength(pageResult.getQuery());
        queryWrapper.like(flag,"name",queryWrapper);
        iPage = bookTempMapper.selectPage(iPage,queryWrapper);

        long total = iPage.getTotal();
        List<BookTemp> rows = iPage.getRecords();
        return pageResult.setTotal(total).setRows(rows);
    }

    @Override
    public BookTemp getBookTemp(Integer id) {
        BookTemp bookTemp = bookTempMapper.selectById(id);
        return bookTemp;
    }

    @Override
    public void addBookTemp(BookTemp bookTemp) {
        Book book = new Book();
        book.setStatus(true).setStage("0").setName(bookTemp.getName()).setAuthor(bookTemp.getAuthor())
            .setPrice(bookTemp.getPrice()).setUploader(bookTemp.getUploader())
            .setZan(0).setDownAddr(null).setDownNum(0).setIntro(bookTemp.getIntro())
            .setFaceAddr(null).setCreated(new Date()).setUpdated(new Date());

        bookMapper.insert(book);


    }

    @Override
    public void deleteBookTemp(Integer id) {
        bookTempMapper.deleteById(id);
    }

    @Override
    public void updateBookTemp(BookTemp bookTemp) {
        bookTempMapper.updateById(bookTemp);
    }


}






